package Meituan;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * 比较排序耗时
 * @author: leiwenkang02@meituan.com
 * @date: 2018/5/22
 * @time: 19:29
 */
public class Test10 {
    private static final Comparator<Stu> SEQUENCE_COMPARATOR = new Comparator<Stu>() {
        @Override
        public int compare(Stu o1, Stu o2) {
            //先按照活动条件优先级排序 单品>品牌>供应商>品类>全店商品
            int idTypeCompareValue = Integer.compare(o1.getId(), o2.getId());
            if (idTypeCompareValue != 0) {
                return idTypeCompareValue;
            }
            //再按照活动类型排序 折扣活动17 优先
            int ageCompareValue = Integer.compare(o1.getAge(), o2.getAge());
            if (ageCompareValue != 0) {
                return ageCompareValue;
            }
            //最后按照活动创建时间，从大到小排序
            return Integer.compare(o2.getHeight(), o1.getHeight());
        }
    };

    public static void main(String[] args) {
        List<Stu> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            int id = new Random().nextInt(4);
            int age = new Random().nextInt(5);
            int height = new Random().nextInt(1000);
            list.add(new Stu(id, age, height));
        }
        long start = System.currentTimeMillis();
        Collections.sort(list, SEQUENCE_COMPARATOR);
        System.out.println(list.size());
        System.out.println(System.currentTimeMillis() - start);
        for (Stu stu : list) {
            System.out.println(stu);
        }
    }
}

class Stu {
    int id;
    int age;
    int  height;

    public Stu(int id, int age, int height) {
        this.id = id;
        this.age = age;
        this.height = height;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return id + ":" + age + ":" + height;
    }
}
